package com.czz.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.czz.manage.client.OssClient;
import com.czz.manage.client.VodClient;
import com.czz.manage.entity.Chapter;
import com.czz.manage.entity.Period;
import com.czz.manage.mapper.ChapterMapper;
import com.czz.manage.mapper.PeriodMapper;
import com.czz.manage.service.ChapterService;
import com.czz.manage.vo.ChapterVo;
import com.czz.manage.vo.PeriodVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: ChapterServiceImpl
 * @Author Czz
 * @Date 2020/1/22
 * @Time 16:22
 **/
@Service
public class ChapterServiceImpl extends ServiceImpl<ChapterMapper, Chapter> implements ChapterService {

    @Autowired
    ChapterMapper chapterMapper;

    @Autowired
    PeriodMapper periodMapper;

    @Autowired
    OssClient ossClient;

    @Autowired
    VodClient vodClient;

    @Override
    @Transactional
    public void deleteChapterById(String id) {

        String OSS_URL = "http://czz-education.oss-cn-shenzhen.aliyuncs.com/";

        //文档地址列表
        List<String> urls = new ArrayList<>();

        //视频id列表
        List<String> videoList = new ArrayList<>();

        //根据章节id查询课时
        QueryWrapper<Period> periodQueryWrapper = new QueryWrapper<>();
        periodQueryWrapper.eq("chapter_id", id);

        List<Period> periodList = periodMapper.selectList(periodQueryWrapper);

        //遍历获取课时文档地址和视频id
        for (int i = 0; i<periodList.size(); i++){
            Period period = periodList.get(i);
            if (period.getDoc() == 1){
                String url = period.getDocUrl().replace(OSS_URL,"").replaceAll("/","&&");
                urls.add(url);
            }
            if (period.getVideo() == 1){
                String videoId = period.getVideoId();
                videoList.add(videoId);
            }
        }

        //批量删除oss中文档
        ossClient.deleteOssFileBatch(urls);

        //批量删除视频点播中视频
        vodClient.deleteVodBatch(videoList);

        //删除章节所属课时
        periodMapper.delete(periodQueryWrapper);

        //根据id删除章节
        chapterMapper.deleteById(id);
    }

    @Override
    @Transactional
    public List<ChapterVo> getNestedListByCourseId(String courseId) {
        List<ChapterVo> chapterVoList = new ArrayList<>();

        //获取课程章节信息
        QueryWrapper<Chapter> chapterQueryWrapper = new QueryWrapper<>();
        chapterQueryWrapper.eq("course_id", courseId);
        chapterQueryWrapper.orderByDesc("chapter_sort");
        chapterQueryWrapper.orderByAsc("gmt_create");
        List<Chapter> chapterList = chapterMapper.selectList(chapterQueryWrapper);

        //获取课程课时信息
        QueryWrapper<Period> periodQueryWrapper = new QueryWrapper<>();
        periodQueryWrapper.eq("course_id", courseId);
        periodQueryWrapper.orderByDesc("period_sort");
        periodQueryWrapper.orderByAsc("gmt_create");
        List<Period> periodList = periodMapper.selectList(periodQueryWrapper);

        //填充嵌套vo
        for (int i = 0; i < chapterList.size(); i++ ){
            Chapter chapter = chapterList.get(i);

            //创建章节vo
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(chapter, chapterVo);
            chapterVoList.add(chapterVo);

            //填充章节vo中课时信息
            ArrayList<PeriodVo> periodVoArrayList = new ArrayList<>();
            for (int j = 0; j < periodList.size(); j++){
                Period period = periodList.get(j);

                if (chapter.getChapterId().equals(period.getChapterId())){
                    //创建课时vo
                    PeriodVo periodVo = new PeriodVo();
                    BeanUtils.copyProperties(period, periodVo);
                    periodVoArrayList.add(periodVo);
                }
            }
            chapterVo.setChildren(periodVoArrayList);
        }
        return chapterVoList;
    }

}
